package activity_trabalhos;

import java.sql.Date;
import java.text.SimpleDateFormat;

import util.TableHeaderTextView;
import util.TableRowTextView;
import activity_alarmes.Alarmes;
import activity_atividades.Atividades;
import activity_disciplinas.Disciplinas;
import activity_provas.Provas;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.Time;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.adjt.droidsapiens.MainHome;
import com.adjt.droidsapiens.R;

import database.DatabaseConnector;

@SuppressLint("SimpleDateFormat")
public class Trabalhos extends Activity {
	Intent it;
	int semestreAtual;
	boolean buscaAtividadesAntigas = false;
	
	public static final int ATIVIDADES = 0;
	public static final int DISCIPLINAS = 1;
	public static final int PROVAS = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trabalhos);
		
		semestreAtual = getIntent().getIntExtra("semestreAtual",0);
		
		Time time = new Time(Time.getCurrentTimezone());
		time.normalize(true);
		time.setToNow();
		long now = time.toMillis(true);
		String sData, sHora;
		SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat hora = new SimpleDateFormat("HH:mm");
		
		Resources r = getResources();
		int pxw = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 220, r.getDisplayMetrics());
		int pxb = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
		int pxz = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, r.getDisplayMetrics());

		DatabaseConnector db = new DatabaseConnector(getBaseContext());
		Cursor c;
		db.open();
		String tables = "Atividade a, Disciplina d";
		String[] column = new String[] { "a.nome nomeAtividade", "a.data dataAtividade", "a.local localAtividade", "d.nome nomeDisciplina", "a.idAtividade idAtividade" };
		String where = "a.idDisciplina = d.idDisciplina AND a.idSemestre = " + semestreAtual + " AND a.tipo = 2 AND ";
		if(!buscaAtividadesAntigas) {
			where += "dataAtividade > "+ now;
		}else
			where += "dataAtividade < "+ now;
		String orderBy = "dataAtividade";

		TableLayout layout = (TableLayout) findViewById(R.id.Trabalhos_TableLayout);

		c = db.buscar(tables, column, where, orderBy);
		
		long miliseg;
		String temp;
		TableRow tr;
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				temp = c.getString(c.getColumnIndex("nomeAtividade"));
				if(temp.length() > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER_HORIZONTAL);
	
					TableHeaderTextView labelnome = new TableHeaderTextView(this);
					labelnome.setText(R.string.nome);
					labelnome.setWidth(pxz);
					tr.addView(labelnome);
					TableRowTextView nomeTrabalhoTxt = new TableRowTextView(this);
					nomeTrabalhoTxt.setWidth(pxw);
					nomeTrabalhoTxt.setText(temp);
					tr.addView(nomeTrabalhoTxt);
					layout.addView(tr);
				}
				
				temp = c.getString(c.getColumnIndex("nomeDisciplina"));
				if(temp.length() > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER_HORIZONTAL);
	
					TableHeaderTextView labeldisciplina = new TableHeaderTextView(this);
					labeldisciplina.setWidth(pxz);
					labeldisciplina.setText(R.string.disciplina);
					tr.addView(labeldisciplina);
					TableRowTextView disciplinaTrabalhoTxt = new TableRowTextView(this);
					disciplinaTrabalhoTxt.setWidth(pxw);
					disciplinaTrabalhoTxt.setText(temp);
					tr.addView(disciplinaTrabalhoTxt);
					layout.addView(tr);
				}
				
				temp = c.getString(c.getColumnIndex("localAtividade"));
				if(temp.length() > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER_HORIZONTAL);
					
					TableHeaderTextView labellocal = new TableHeaderTextView(this);
					labellocal.setWidth(pxz);
					labellocal.setText(R.string.local);
					tr.addView(labellocal);
					TableRowTextView localTrabalhoTxt = new TableRowTextView(this);
					localTrabalhoTxt.setWidth(pxw);
					localTrabalhoTxt.setText(temp);
					tr.addView(localTrabalhoTxt);
					layout.addView(tr);
				}

				miliseg = c.getLong(c.getColumnIndex("dataAtividade"));
				if(miliseg > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER_HORIZONTAL);
					
					TableHeaderTextView labeldata = new TableHeaderTextView(this);
					labeldata.setWidth(pxz);
					labeldata.setText(R.string.data);
					tr.addView(labeldata);
					sData = data.format(new Date(miliseg));
					TableRowTextView dataTrabalhoTxt = new TableRowTextView(this);
					dataTrabalhoTxt.setWidth(pxw);
					dataTrabalhoTxt.setText(sData);
					tr.addView(dataTrabalhoTxt);
					layout.addView(tr);
				}
				
				if(miliseg > 0){
					tr = new TableRow(this);
					tr.setGravity(Gravity.CENTER_HORIZONTAL);
					
					TableHeaderTextView labelhora = new TableHeaderTextView(this);
					labelhora.setWidth(pxz);
					labelhora.setText(R.string.hora);
					tr.addView(labelhora);
					sHora = hora.format(new Date(miliseg));
					TableRowTextView horaTrabalhoTxt = new TableRowTextView(this);
					horaTrabalhoTxt.setWidth(pxw);
					horaTrabalhoTxt.setText(sHora);
					tr.addView(horaTrabalhoTxt);
					layout.addView(tr);
				}
				
				LinearLayout ll = new LinearLayout(this);
				ll.setGravity(Gravity.CENTER);
				
				Button verTrabalho = new Button(this);
				verTrabalho.setTag(c.getString(c.getColumnIndex("idAtividade")));
				verTrabalho.setBackgroundResource(R.drawable.ic_view);
				verTrabalho.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						it = new Intent(getBaseContext(), ExibirTrabalho.class);
						it.putExtra("idAtividade", Integer.parseInt(v.getTag().toString()));
						it.putExtra("semestreAtual", semestreAtual);
						startActivity(it);
						finish();
					}
				});
				verTrabalho.setHeight(pxb);
				verTrabalho.setWidth(pxb);
				ll.addView(verTrabalho);
				
				Button addAlarme = new Button(this);
				addAlarme.setTag(c.getString(c.getColumnIndex("idAtividade")));
				addAlarme.setBackgroundResource(R.drawable.ic_alarm);
				addAlarme.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						it = new Intent(getBaseContext(), Alarmes.class);
						it.putExtra("idAtividade", Integer.parseInt(v.getTag().toString()));
						it.putExtra("semestreAtual", semestreAtual);
						startActivity(it);
						finish();
					}
				});
				addAlarme.setHeight(pxb);
				addAlarme.setWidth(pxb);
				ll.addView(addAlarme);
				layout.addView(ll);
				
				tr = new TableRow(this);
				TextView vazia = new TextView(this);
				tr.addView(vazia);
				layout.addView(tr);
			}
		}else {
			tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER);
			
			TextView listaVazia = new TextView(this);
			listaVazia.setText(R.string.listaTrabalhosVazia);
			tr.addView(listaVazia);
			
			layout.addView(tr);
		}
		db.close();
	}

	public void goNovaTrabalho(View v) {
		it = new Intent(getBaseContext(), NovoTrabalho.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}
	
	@Override
	public void onBackPressed(){
		it = new Intent(getBaseContext(), MainHome.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, ATIVIDADES, 1, R.string.atividades);
		menu.add(0, DISCIPLINAS, 2, R.string.disciplinas);
		menu.add(0, PROVAS, 3, R.string.provas);
				
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featuredId, MenuItem item){
		
		switch(item.getItemId()){
		
			case ATIVIDADES:
				it = new Intent(getBaseContext(), Atividades.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;
			  
			case DISCIPLINAS:
				it = new Intent(getBaseContext(), Disciplinas.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;
				  
			case PROVAS:
				it = new Intent(getBaseContext(), Provas.class);
				it.putExtra("semestreAtual", semestreAtual);
				startActivity(it);
				finish();
				return true;	  
		}
		return false;
		
	}
}
